package org.kalai.unifypay.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.kalai.unifypay.Constant;
import org.kalai.unifypay.bean.Member;
import org.kalai.unifypay.bean.Settlement;
import org.kalai.unifypay.util.CipherUtils;
import org.kalai.unifypay.util.CommonUtil;
import org.kalai.unifypay.util.DataDao;
import org.kalai.unifypay.util.DateUtil;
import org.kalai.unifypay.util.HttpUtil;
import org.kalai.unifypay.util.ProxyPayUtil;

import com.alibaba.fastjson.JSON;

@WebServlet(urlPatterns="/TransactionSettlement")
public class TransactionSettlement extends HttpServlet{
	private static String DATE = "";
	private static final Logger logger = Logger.getLogger(TransactionSettlement.class);
	private static final long serialVersionUID = 6839760340727946550L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		CommonUtil.responseObject(resp, Constant.ERROR_REQEST);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String pddate = DateUtil.formatNow("yyyyMMddHH");
		if(DATE.equals(pddate)){
			logger.error("T1代付一小时内不能重复请求");
			CommonUtil.responseObject(response, JSON.toJSONString(CommonUtil.createRespMap("200", "代付一小时内不能重复请求")));
			return;
		}
		DATE = pddate;
		String signData = request.getParameter("signData");
		String date = DateUtil.formatNow("yyyyMMdd");
		if(!CipherUtils.encryptMD5(date).equalsIgnoreCase(signData)){
			logger.error("签名验证错误");
			CommonUtil.responseObject(response, JSON.toJSONString(CommonUtil.createRespMap("200", "签名验证错误")));
			return;
		}
		new Thread(new Runnable() {
			@Override
			public void run() {
				logger.info("开始批量代付");
				List<Settlement> settlements = DataDao.getInstance().getSettlement();
				if (settlements.size() == 0) logger.info("无需代付");
				logger.info(settlements.size());
				for (Settlement settlement : settlements) {
					logger.info(settlement.getMemberNo());
					try {
						Member member = DataDao.getInstance().getMember(settlement.getMemberNo());
						ProxyPayUtil.proxyPay(settlement.getSettNo(), member, ((Double)(settlement.getSettAmt()*100)).longValue(), settlement.getBizcode(), false);
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			}
		}).start();
		CommonUtil.responseObject(response, JSON.toJSONString(CommonUtil.createRespMap("00", "请求成功")));
	}
	
	public static void main(String[] args) {
		String url = "http://open.qrcodexx.com:8001/unifypay/TransactionSettlement";
		String signData = CipherUtils.encryptMD5(DateUtil.formatNow("yyyyMMdd"));
		Map<String, Object> params = new HashMap<>();
		params.put("signData", signData);
		System.out.println(HttpUtil.sendPost(url, params));
	}
}
